package com.xinyi;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/3/13
 */
public class Service {
    private IDatabaseProvider databaseProvider;
    private IThirdpartyAPIProvider apiProvider;
    private IThirdpartyAuthenticationProvider authenticationProvider;

    public Service(IDatabaseProvider databaseProvider, IThirdpartyAPIProvider apiProvider, IThirdpartyAuthenticationProvider authenticationProvider) {
        this.databaseProvider = databaseProvider;
        this.apiProvider = apiProvider;
        this.authenticationProvider = authenticationProvider;
    }

    public void connection(String connectionString) {
        databaseProvider.connection(connectionString);
    }

    public String executeSql(String query) {
        ResultSet resultSet= databaseProvider.executeSql(query);
        try {
            // 检查是否有结果并获取第一个 token
            if (resultSet != null && resultSet.next()) {  // 使用 next() 方法移动到第一行
                String token = resultSet.getString("token");  // 获取 token 列的值
                System.out.println("第一个 token: " + token);  // 打印第一个 token
                return token;
            } else {
                System.out.println("没有找到 token！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void getDataFromAPI(String apiUrl, String params) {
        apiProvider.getDataFromAPI(apiUrl, params);
    }

    public boolean login(String token) {
        boolean flag=authenticationProvider.login(token);
        if(flag==true){
            System.out.println("登录成功");
            return true;
        }else{
            System.out.println("登录失败");
            return false;
        }

    }
}

